package com.gxa.meiye.admin.shop.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.meiye.admin.shop.service.ServiceManagement;
import com.gxa.meiye.core.common.ResponseData;
import com.gxa.meiye.core.constant.MassageConstant;
import com.gxa.meiye.core.pojo.shopManagement.Project;
import com.gxa.meiye.core.utils.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author cc
 * @PackageName meiye
 * @Package com.gxa.meiye.admin.shop.controller
 * @Date 2022/7/24 17:59
 * @Version 1.0
 */
@RestController
@RequestMapping("shopService")
@Api(tags = "服务管理接口")
public class AdminServiceController {
 @Autowired
 private ServiceManagement serviceManagement;
 @ApiOperation("查看所有服务项目")
 @GetMapping("allService")
    public ResponseData getAllService(){
     ResponseData rd = new ResponseData();
     Integer total = serviceManagement.getAllServiceCount();
     List<Project> allService = serviceManagement.getAllService();
     rd.setCode(200);
     rd.setMsg("查询成功");
     rd.setCount(total);
     rd.setData(allService);
     return rd;
 }
    @ApiOperation("查看上架中和未上架的项目的服务项目 1代表上架中 2代表下架")
    @GetMapping("checkService")
    public IPage<Project> getShelf(@RequestParam Integer pageNum,
                                      @RequestParam  Integer pageSize,
                                      @RequestParam Integer projectStatus ){
        IPage<Project> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",projectStatus);
        IPage<Project> ShelfPage = serviceManagement.page(page, queryWrapper);

     return ShelfPage;
    }
    @ApiOperation("输入项目名称查看")
    @GetMapping("projectName")
    public IPage<Project> getProjectName(@RequestParam Integer pageNum,
                                        @RequestParam  Integer pageSize,
                                        @RequestParam String name ) {
        IPage<Project> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        IPage<Project> ProjectNamePage = serviceManagement.page(page, queryWrapper);

        return ProjectNamePage;

    }

    @ApiOperation("根据项目名称新增或编辑项目")
    @PostMapping("addProject")
    public ResponseData addProject(MultipartFile imagFile1,Project project) {
     ResponseData rd = new ResponseData();
        try{
            //1.获取原始第一个文件名
            String originalFilename1 = imagFile1.getOriginalFilename();
            //2.生成新的文件名称
            int index1 = originalFilename1.lastIndexOf(".");//djx.jpg
            String suffix1 = originalFilename1.substring(index1);//.jpg
            String filename1 = UUID.randomUUID().toString() + suffix1;
            //3.调用工具类，上传图片到七牛云
            QiniuUtils.upload2Qiniu(imagFile1.getBytes(), filename1);
            project.setImage(filename1);
        }catch (Exception e) {
            e.printStackTrace();
            rd.setMsg(MassageConstant.PIC_UPLOAD_FAIL);
        }
        String name = project.getName();
        //根据projectNumber 查询
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String todayTime = sdf.format(date);
        project.setProjectNumber(todayTime);
        Integer number = serviceManagement.getByProjectName(name);
        if(number!=0){
            rd.setCode(200);
            rd.setMsg("修改项目成功");
            serviceManagement.updateByProjectName(project);
        }else {
            rd.setCode(200);
            rd.setMsg("新增项目成功");
            serviceManagement.saveAll(project);
        }

        return rd;

    }
    @ApiOperation("上架支持批量")
    @PutMapping("putOnSale")
    public ResponseData  putOnSaleStatus(@RequestBody Integer[] ids){
        ResponseData putOnSale = serviceManagement.putOnSale(ids);
        return putOnSale;
    }

    @ApiOperation("下架支持批量")
    @PutMapping("offTheShelf")
    public ResponseData  offTheShelfStatus(@RequestBody Integer[] ids){
        ResponseData offTheShelf = serviceManagement.offTheShelf(ids);
        return offTheShelf;
    }
    @ApiOperation("根据name编辑")
    @PutMapping("editByName")
    public ResponseData editProjectById(MultipartFile imagFile1,Project project){
     ResponseData rd = new ResponseData();
        if(null!=imagFile1){
            try{
                //1.获取原始第一个文件名
                String originalFilename1 = imagFile1.getOriginalFilename();
                //2.生成新的文件名称
                int index1 = originalFilename1.lastIndexOf(".");//djx.jpg
                String suffix1 = originalFilename1.substring(index1);//.jpg
                String filename1 = UUID.randomUUID().toString() + suffix1;
                //3.调用工具类，上传图片到七牛云
                QiniuUtils.upload2Qiniu(imagFile1.getBytes(), filename1);
                project.setImage(filename1);
            }catch (Exception e) {
                e.printStackTrace();
                rd.setMsg(MassageConstant.PIC_UPLOAD_FAIL);
            }
     }
        String name = project.getName();
        Integer number = serviceManagement.getByProjectName(name);
        if(number!=0){
            serviceManagement.changeProject(project);
            rd.setCode(200);
            rd.setMsg("编辑成功");
        }else{
            rd.setMsg("没有找到该项目,如果需要请新增项目！");
        }
              return rd;
    }

    @ApiOperation("根据id删除")
    @PutMapping("deleteById")
    public ResponseData deleteProjectById(Integer id){
     ResponseData rd = new ResponseData();
         serviceManagement.removeById(id);
          rd.setCode(200);
          rd.setMsg("删除成功");
        return rd;
    }

}
